﻿#include <QSettings>
#include <QTextCodec>
#include <QStandardPaths>

#include "QtMapWidget/QtMapWidget.h"
#include "AppSettings.h"

AppSettings::AppSettings(QObject *parent)
    : QObject{parent}
{
    _writableLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
    _file = QString("%1/%2.ini").arg(_writableLocation, WidgetName);

}

QVariant AppSettings::value(const QString &key, const QVariant &defaultValue) const
{
    QSettings* settings = new QSettings(_file, QSettings::IniFormat);
    settings->setIniCodec(QTextCodec::codecForName("UTF-8"));

    return settings->value(key, defaultValue);
}

void AppSettings::setValue(const QString &key, const QVariant &value)
{
    QSettings* settings = new QSettings(_file, QSettings::IniFormat);
    settings->setIniCodec(QTextCodec::codecForName("UTF-8"));

    settings->setValue(key, value);
    settings->sync();
}
